#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1

NAME=natmap
PROG=/usr/bin/$NAME

STATUS_PATH=/var/run/natmap

load_interfaces() {
	config_get interface "$1" interface
	config_get enable "$1" enable 1

	[ "${enable}" = "1" ] && interfaces=" ${interface} ${interfaces}"
}

validate_section_natmap() {
	uci_load_validate "${NAME}" natmap "$1" "$2" \
		'enable:bool:0' \
		'family:string' \
		'mode:string:general' \
		'udp_mode:bool:0' \
		'interface:string' \
		'nat_name:string' \
		'interval:uinteger' \
		'stun_server:host' \
		'http_server:host' \
		'port:port' \
		'forward_target:host' \
		'forward_port:port' \
		'notify_script:file' \
		'forward_use_natmap:bool:0' \
		'im_notify_enable:bool:0' \
		'im_notify_channel:string' \
		'im_notify_channel_telegram_bot_chat_id:string' \
		'im_notify_channel_telegram_bot_token:string' \
		'im_notify_channel_pushplus_token:string' \
		'emby_url:string' \
		'emby_api_key:string' \
		'emby_use_https:bool:0' \
		'emby_update_host_with_ip:bool:0' \
		'qb_web_ui_url:string' \
		'qb_username:string' \
		'qb_password:string' \
		'qb_ipv6_address:string' \
		'qb_allow_ipv6:bool:0' \
		'tr_rpc_url:string' \
		'tr_username:string' \
		'tr_password:string' \
		'tr_allow_ipv6:bool:0' \
		'tr_ipv6_address:string' \
		'cloudflare_email:string' \
		'cloudflare_api_key:string' \
		'cloudflare_zone_id:string' \
		'cloudflare_rule_target_url:string' \
		'cloudflare_rule_name:string' \
		'proxy_port_content_url:string' \
		'proxy_port_gist_id:string' \
		'proxy_port_gist_filename:string' \
		'proxy_port_github_token:string' \
		'proxy:string'
}

natmap_instance() {
	[ "$2" = 0 ] || {
		echo "validation failed"
		return 1
	}

	[ "${enable}" = 0 ] && return 1

	procd_open_instance "$1"
	procd_set_param command "$PROG" \
		${interval:+-k "$interval"} \
		${stun_server:+-s "$stun_server"} \
		${http_server:+-h "$http_server"} \
		${port:+-b "$port"} \

	[ "${family}" = ipv4 ] && procd_append_param command -4
	[ "${family}" = ipv6 ] && procd_append_param command -6
	[ "${udp_mode}" = 1 ] && procd_append_param command -u

	[ -n "${interface}" ] && {
		local ifname

		network_get_device ifname "$interface" || ifname="$interface"
		procd_append_param command -i "$ifname"
		procd_append_param netdev "$ifname"
	}

	[ -n "${forward_target}" ] && [ $forward_use_natmap == "1" ] && procd_append_param command -t "$forward_target" -p "$forward_port"
	for e in $EXTRA_ENV; do
		eval value=\$${e}
		option_name=$(echo "$e" | tr 'a-z' 'A-Z')
		if [ -n "$value" ]; then
			procd_append_param env $option_name="$value"
		fi
	done
	[ $mode == 'qbittorrent' ] && procd_append_param env INTERNAL_DEFINE_SCRIPT=/usr/lib/natmap/plugin/qb.sh
	[ $mode == 'transmission' ] && procd_append_param env INTERNAL_DEFINE_SCRIPT=/usr/lib/natmap/plugin/tr.sh
	[ $mode == 'emby' ] && procd_append_param env INTERNAL_DEFINE_SCRIPT=/usr/lib/natmap/plugin/emby.sh
	[ $mode == 'cloudflare_origin_rule' ] && procd_append_param env INTERNAL_DEFINE_SCRIPT=/usr/lib/natmap/plugin/cloudflare_origin_rule.sh
	[ $mode == 'cloudflare_redirect_rule' ] && procd_append_param env INTERNAL_DEFINE_SCRIPT=/usr/lib/natmap/plugin/cloudflare_redirect_rule.sh
	[ $mode == 'proxy_port' ] && procd_append_param env INTERNAL_DEFINE_SCRIPT=/usr/lib/natmap/plugin/proxy_port.sh
	procd_append_param command -e /usr/lib/natmap/update.sh

	procd_set_param respawn
	procd_set_param stdout 1
	procd_set_param stderr 1

	procd_close_instance
}

clear_status_files() {
	find "${STATUS_PATH}" -type f -print0 | xargs -0 rm -f --
}

service_triggers() {
	local interfaces

	procd_add_reload_trigger "${NAME}"

	config_load "${NAME}"
	config_foreach load_interfaces natmap

	[ -n "${interfaces}" ] && {
		for n in $interfaces ; do
			procd_add_reload_interface_trigger $n
		done
	}

	procd_add_validation validate_section_natmap
}

start_service() {
	. /lib/functions/network.sh
	config_cb() {
		[ $# -eq 0 ] && return

		option_cb() {
			local option="$1"

			EXTRA_ENV="$EXTRA_ENV $option"
		}

		list_cb() {
			local name="$1"
			EXTRA_ENV="$EXTRA_ENV $name"
		}
	}
	mkdir -p "${STATUS_PATH}"
	clear_status_files

	config_load "${NAME}"
	config_foreach validate_section_natmap natmap natmap_instance
}

reload_service() {
	stop
	start
}

service_stopped() {
	clear_status_files
}
